import os
import json
import snbtlib

def snbt_to_json(snbt_file_path, json_file_path):
    with open(snbt_file_path, 'r') as snbt_file:
        snbt_data = snbt_file.read()
    
    json_data = snbtlib.loads(snbt_data)
    
    with open(json_file_path, 'w', encoding='utf-8') as json_file:
        json.dump(json_data, json_file, indent=4, ensure_ascii=False)

def process_directory(directory):
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.snbt'):
                snbt_file_path = os.path.join(root, file)
                json_file_path = os.path.join(root, file[:-5] + '.json')
                
                try:
                    snbt_to_json(snbt_file_path, json_file_path)
                    print(f"Converted {snbt_file_path} to {json_file_path}")
                    
                    # Delete the original .snbt file
                    os.remove(snbt_file_path)
                    print(f"Deleted {snbt_file_path}")
                except Exception as e:
                    print(f"Error processing {snbt_file_path}: {str(e)}")

if __name__ == "__main__":
    input_directory = os.path.join(os.path.dirname(os.path.abspath(__file__)), "input")
    
    if not os.path.exists(input_directory):
        print(f"Error: {input_directory} does not exist.")
    else:
        process_directory(input_directory)
        print("Conversion completed.")


